package com.yaopaikeji.mapper;

import com.yaopaikeji.model.EmailGroupInfo;
import com.yaopaikeji.model.User;
import com.yaopaikeji.model.UserGroupRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserGroupRelationMapper {

    //根据主键查询关系
    @Select("select * from user_group_relation where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "user_id",property = "user",
                    one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey")),
            @Result(column = "group_id",property = "emailGroupInfo",
                    one = @One(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findGroupByPrimaryKey"))

    })
    UserGroupRelation findRelationByPrimarykey(int id);

    //查询所有关系
    @Select("select * from user_group_relation")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "user_id",property = "user",
                    one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey")),
            @Result(column = "group_id",property = "emailGroupInfo",
                    one = @One(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findGroupByPrimaryKey"))

    })
    List<UserGroupRelation> findRealationsAll();

    //根据groupId查询所有的user信息
    @Select("select * from user u inner join " +
            "user_group_relation ugr on u.id = ugr.user_id " +
            "where ugr.group_id = #{groupId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "login_id",property = "loginId"),
            @Result(column = "pass",property = "pass"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "is_active",property = "isActive"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_login_time",property = "lastLoginTime"),
            @Result(column = "role_id",property = "roleInfo",
                    one = @One(select = "com.yaopaikeji.mapper.RoleInfoMapper.findByPrimaryKey"))
    })
    List<User> findUsersByGroupId(int groupId);

    //根据userId查询所在的邮件组
    @Select("select * from email_group_info egi inner join user_group_relation ugr on egi.id = ugr.group_id where ugr.user_id = #{userId}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "group_name",property = "groupName"),
            @Result(column = "group_desc",property = "groupDesc"),
            @Result(column = "create_time",property = "createTime")
    })
    List<EmailGroupInfo> findEmailGroups(int userId);

    //添加user进邮件组

    @Insert("insert into user_group_relation(    " +
            "user_id,group_id,create_time    " +
            ")    " +
            "values(    " +
            "#{user.id},#{emailGroupInfo.id},#{createTime}    " +
            ");")
    int insertRelation(UserGroupRelation userGroupRelation);

    @Insert("<script>" +
            "    insert into user_group_relation" +
            "    (" +
            "        user_id,group_id,create_time" +
            "    )" +
            "    values" +
            "    <foreach item='item' collection='list' separator=','>" +
            "        (" +
            "            #{item.user.id}," +
            "            #{item.emailGroupInfo.id}," +
            "            now()" +
            "        )" +
            "    </foreach>" +
            "</script>")
    int insertRelations(List<UserGroupRelation> userGroupRelations);

    //解除关系
    @Delete("delete from user_group_relation where user_id = #{userId} and group_id = #{groupId}")
    int deleteRelation(@Param("userId") int userId,@Param("groupId") int groupId);

    //根据主键解除关系
    @Delete("delete from user_group_relation where id = #{id}")
    int deleteRelationByPrimaryKey(int id);

    //删除邮件组内成员
    @Delete("delete from user_group_relation where group_id = #{id}")
    int deleteEmailGroupRelation(int groupId);

    //删除某个人和邮件组的关联
    @Delete("delete from user_group_relation where user_id = #{userId}")
    int deleteRelationByUserId(int userId);
}
